package com.houger.ddd.common.base;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.io.Serializable;

/**
 * @author Houger.
 * @date 2024年04月20日 16:03.
 */
public abstract class BaseDto implements Serializable, Cloneable{

    /**
     * to string  SerializerFeature
     */
    private static final SerializerFeature[] TO_STRING_SERIALIZER_FEATURES = new SerializerFeature[]{SerializerFeature.WriteMapNullValue,
        SerializerFeature.WriteNullListAsEmpty,
        SerializerFeature.WriteNullStringAsEmpty,
        SerializerFeature.SortField,
        SerializerFeature.WriteDateUseDateFormat};

    /**
     * to NotNull string SerializerFeature
     */
    private static final SerializerFeature[] TO_NO_NULL_STRING_SERIALIZER_FEATURES = new SerializerFeature[]{SerializerFeature.WriteNullListAsEmpty,
        SerializerFeature.WriteDateUseDateFormat};

    public String toStringNotNull() {
        return JSON.toJSONString(this, TO_NO_NULL_STRING_SERIALIZER_FEATURES);
    }


    @Override
    public int hashCode() {
        return this.toString().hashCode();
    }


    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        return this.toString().equals(obj.toString());
    }


    @Override
    public String toString() {
        return JSON.toJSONString(this, TO_STRING_SERIALIZER_FEATURES);
    }

    @Override
    public BaseDto clone() {
        try {
            // TODO: copy mutable state here, so the clone can't change the internals of the original
            return (BaseDto) super.clone();
        } catch (CloneNotSupportedException e) {
            throw new AssertionError();
        }
    }
}
